import { sts } from 'tencentcloud-sdk-nodejs-sts'
import { Router } from 'express'

const StsClient = sts.v20180813.Client;
const clientConfig = {
  credential: {
    secretId: process.env.SECRET_ID,
    secretKey: process.env.SECRET_KEY,
  },
  region: "ap-shanghai",
  profile: {
    httpProfile: {
      endpoint: "sts.tencentcloudapi.com",
    },
  },
};
const client = new StsClient(clientConfig);
const downloadParams = {
  Name: "static-1302789012",
  // 不填默认 1800s，30分钟
  // DurationSeconds: 1800,
  Policy: encodeURIComponent(JSON.stringify({
    version: "2.0",
    statement: [{
      effect: "allow",
      action: [
        'name/cos:GetBucket',
        'name/cos:GetObject'
      ],
      resource: "qcs::cos:ap-shanghai:uid/1302789012:static-1302789012/personal/pictures/*"
    }]
  }))
}
const uploadParams = {
  Name: "static-1302789012",
  // 不填默认 1800s，30分钟
  // DurationSeconds: 1800,
  Policy: encodeURIComponent(JSON.stringify({
    version: "2.0",
    statement: [{
      effect: "allow",
      action: [
        "name/cos:PutObject",
        "name/cos:DeleteObject",
        'name/cos:InitiateMultipartUpload',
        'name/cos:ListMultipartUploads',
        'name/cos:ListParts',
        'name/cos:UploadPart',
        'name/cos:CompleteMultipartUpload'
      ],
      resource: "qcs::cos:ap-shanghai:uid/1302789012:static-1302789012/temp/*"
    }]
  }))
}

export function getCloudAlbumSts () {
  return client.GetFederationToken(downloadParams)
}

export function getClientSts () {
  return client.GetFederationToken(uploadParams)
}

const router = Router()
router.use('/getClientSts', (req, res) => {
  getClientSts().then(data => res.send(data))
})
export default router
